﻿using HttpApiGateway.Idempotent;
using HttpApiGateway.Interface;
using HttpApiGateway.Model;
using RpcClient.Interface;
using RpcHelper;
using System;

namespace HttpApiGateway.Config
{

    [RpcClient.Attr.ClassLifetimeAttr(RpcClient.Attr.ClassLifetimeType.单例)]
    internal class IdempotentConfig : IIdempotentConfig
    {
        private bool _IsLimit = false;
        private string[] _Routes;
        public IdempotentConfig(ISysConfig config)
        {
            config.AddRefreshEvent(_Refresh);
            _Init(config.Config);
        }

        public event Action<IIdempotentConfig> RefreshEvent;

        private void _Init(IConfigServer config)
        {
            this.IdempotentType = config.GetConfigVal<IdempotentType>("gateway:idempotent:Type", IdempotentType.关闭);
            this.TokenRoute = config.GetConfigVal<RouteSet>("gateway:idempotent:Route",new RouteSet { 
                IsAccredit=true,
                RoutePath= "/api/idempotent/token",
                IsRegex=false
            });
            this.IsEnableRoute = config.GetConfigVal<bool>("gateway:idempotent:IsEnableRoute", true);
            this.Expire = config.GetConfigVal<int>("gateway:idempotent:Expire", 30);
            this.SaveType = config.GetConfigVal<StatusSaveType>("gateway:idempotent:SaveType", StatusSaveType.Local);
            this.ArgName = config.GetConfigVal("gateway:idempotent:ArgName", "tokenId");
            this.RequestMehod = config.GetConfigVal<RequestMehod>("gateway:idempotent:Method", RequestMehod.Head);
            string[] routes = config.GetConfigVal<string[]>("gateway:idempotent:Routes");
            if (routes.IsNull())
            {
                _IsLimit = false;
            }
            else
            {
                this._Routes = routes;
                this._IsLimit = true;
            }
        }
        private void _Refresh(IConfigServer config, string name)
        {
            if (name == "gateway:idempotent" || name.StartsWith("gateway:idempotent:"))
            {
                _Init(config);
                RefreshEvent?.Invoke(this);
            }

        }

        public bool CheckIsLimit(string path)
        {
            return this._IsLimit && this._Routes.IsExists(path);
        }

        public IdempotentType IdempotentType
        {
            get;
            private set;
        }
        public string ArgName
        {
            get;
            private set;
        }
        public RequestMehod RequestMehod
        {
            get;
            private set;
        }
        public RouteSet TokenRoute
        {
            get;
            private set;
        }
        /// <summary>
        /// Token过期时间(秒)
        /// </summary>
        public int Expire
        {
            get;
            private set;
        }
        /// <summary>
        /// 存储方式
        /// </summary>
        public StatusSaveType SaveType
        {
            get;
            private set;
        }

        public bool IsEnableRoute
        {
            get;
            private set;
        }
    }
}
